package com.da.javatest.interview.session2;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author chenlida
 * @date 2020/3/29 15:47
 *
 * 故障：java.util.ConcurrentModificationException
 * 导致原因：并发争抢修改导致
 * 解决方法：1.Vector<>() 2.Collections.synchronizedList(new ArrayList<>()) 3.CopyOnWriteArrayList<>()
 */
public class Test07ArrayList {
    public static void main(String[] args) {
        //List<String> list = new ArrayList<>();
        //List<String> list = new Vector<>();
        //List<String> list = Collections.synchronizedList(new ArrayList<>());
        List<String> list = new CopyOnWriteArrayList<>();

        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                String uuid = UUID.randomUUID().toString().substring(0, 8);
                list.add(uuid);
                System.out.println(list);
            }).start();
        }
    }
}
